package com.shenbinhong.proxyip.service.impl;

import com.shenbinhong.proxyip.Enum.IsForeign;
import com.shenbinhong.proxyip.Enum.ProxyServer;
import com.shenbinhong.proxyip.entity.ProxyIPEntity;
import com.shenbinhong.proxyip.service.XiciProxyService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 123430134@qq.com on 2016/6/2.
 */
public abstract class
XiciProxyServiceBaseImpl implements XiciProxyService {
    /**
     * 用户客户端常亮
     */
    public static final String USERAGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2334.0 Safari/537.36";

    public Elements getProxyElements(ProxyServer proxyServer) {
        Document doc = null;
        Elements list = null;
        try{
            doc = Jsoup.connect(proxyServer.getServerUrl()).userAgent(USERAGENT).timeout(3000).get();
            Element tbody = doc.getElementsByTag("tbody").get(0);
            list = tbody.children();
            list.remove(0);
            return list;
        }catch (IOException e){
            return list;
        }
//        return list;
    }


    public List<ProxyIPEntity> extraProxyElements(Elements list,IsForeign isForeign) {
        if(list==null||list.size()==0){
            return null;
        }
        List<ProxyIPEntity> ips = new ArrayList<ProxyIPEntity>();
        ProxyIPEntity proxyIPEntity = null;
        int i =0;
        for(Element element:list){
            proxyIPEntity = new ProxyIPEntity();
//            System.out.println(++i);
//            System.out.println("========================================");
            Element ip = element.child(1);
//            System.out.println("ip:"+ip.html());
            proxyIPEntity.setHost(ip.html());

            Element port = element.child(2);
//            System.out.println("port:"+port.html());
            proxyIPEntity.setPort(Integer.parseInt(port.html()));

            Element address = element.child(3);
            if(isForeign==IsForeign.FALSE){
//                System.out.println("address:"+((address.children().size()>0)?address.child(0).html():""));
                proxyIPEntity.setAddress(((address.children().size()>0)?address.child(0).html():""));
            }else{
//                System.out.println("address:"+address.html());
                proxyIPEntity.setAddress(address.html());
            }

            Element isHide = element.child(4);
//            System.out.println("isHide:"+isHide.html());
            proxyIPEntity.setIsHide(isHide.html());

            Element type = element.child(5);
//            System.out.println("type:"+type.html());
            proxyIPEntity.setType(type.html());

            Element speed = element.child(6);
//            System.out.println("speed:"+speed.child(0).attr("title"));
            String speedTitle = speed.child(0).attr("title");
            if(speedTitle.contains("秒")){
                speedTitle = speedTitle.substring(0,speedTitle.indexOf("秒"));
            }
            proxyIPEntity.setSpeed(Float.parseFloat(speedTitle));

            Element connectTime = element.child(7);
//            System.out.println("connectTime:"+connectTime.child(0).attr("title"));
            String connectTimeTitle = connectTime.child(0).attr("title");
            if(connectTimeTitle.contains("秒")){
                connectTimeTitle = connectTimeTitle.substring(0,connectTimeTitle.indexOf("秒"));
            }
            proxyIPEntity.setConnectTime(Float.parseFloat(connectTimeTitle));

            Element aliveTime = element.child(8);
//            System.out.println("aliveTime:"+aliveTime.html());
            String aliveTimeStr = aliveTime.html();
            if(aliveTimeStr.contains("天")){
                aliveTimeStr = aliveTimeStr.substring(0,aliveTimeStr.indexOf("天"));
                proxyIPEntity.setAliveTime(Integer.parseInt(aliveTimeStr)*24*60);
            }else if(aliveTimeStr.contains("小时")){
                aliveTimeStr = aliveTimeStr.substring(0,aliveTimeStr.indexOf("小时"));
                proxyIPEntity.setAliveTime(Integer.parseInt(aliveTimeStr)*60);
            }else if(aliveTimeStr.contains("分钟")){
                aliveTimeStr = aliveTimeStr.substring(0,aliveTimeStr.indexOf("分钟"));
                proxyIPEntity.setAliveTime(Integer.parseInt(aliveTimeStr));
            }

            Element verifyTime = element.child(9);
//            System.out.println("verifyTime:"+verifyTime.html());
            proxyIPEntity.setVerifyTime(verifyTime.html());
            ips.add(proxyIPEntity);
        }

        return ips;
    }
}
